Array Programming

Table of Contents

Odin has built-in supports array and matrix arithmetic.

1. Slices

Slices look like arrays, but their length is not known at compile time. Slices have type []T where T is the value type.

A slice is formed by specifying two indices, an inclusive lower bound and an exclusive upper bound.

  a[low: high]

Slice Implementation. A slice stores a pointer to the data and an interger to store the length of the slice.

1.1. Slice Literals vs. Array Literals

Array literals specifies the array length, e.g., [3]int{1,2,3}; while slice literals do not. Indeed, Odin first creates an array, then creates a slice that reference it.

1.2. Dynamic Arrays

Dynamic arrays are similar to slices, but their length may change during runtime. They are allocated using the current context’s allocator.

  • append(&arr, ..vals) appends values to the end of array
x := make([dynamic]int, 0, 16)
inject_at(&x, 0, 10)
inject_at(&x, 3, 10) // resizes till length
fmt.eprintln(x[:], len(x), cap(x)) // [10, 0, 0, 10] 4 16
assign_at(&x, 3, 20)
assign_at(&x, 4, 30)
fmt.eprintln(x[:], len(x), cap(x)) // [10, 0, 0, 20, 30] 5, 16
assign_at(&x, 5, 40, 50, 60)
fmt.eprintln(x[:], len(x), cap(x)) // [10, 0, 0, 20, 30, 40, 50, 60] 8 16
  • pop() removes the last element
  • ordered_remove(&arr, index) will move all elements after the index downwards with copy
  • unordered_remove(&arr, index) is \(O(1)\) removal, as it swaps with the last element and pop.

1.3. Fixed Capacity Dynamic Arrays

It implements most dynamic array procedures while being able to remain on stack.

x: [dynamic; 8]int
fmt.println(len(x), cap(x)) // 0 8
append(&x, 1, 2, 3)
fmt.println(len(x), cap(x)) // 3 8
fmt.println(x[:]) // [1, 2, 3]

Date: 2026-06-11 Thu